package decrypt;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.SecretKey;

import decrypt.utils.Constants;
import decrypt.utils.Dec;

/**
 * Main class
 * 
 */
public class Decrypt
{

    /**
     * @param args
     * @throws InvalidKeyException
     * @throws InvalidKeySpecException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws InvalidAlgorithmParameterException
     * @throws IOException
     */
    public static void main( String[] args ) throws InvalidKeyException,
            NoSuchAlgorithmException, NoSuchProviderException,
            InvalidKeySpecException, InvalidAlgorithmParameterException,
            IOException {
        byte[] c_aes_cbc = Constants.getBytesFromFile( Constants.TARGET );
        byte[] c_rsa_oaepwithsha256andmgf1 = Constants
                .getBytesFromFile( Constants.AES_FILENAME );
        byte[] iv = Constants.getBytesFromFile( Constants.IV_FILENAME );
        byte[] rsaPriKey = Constants
                .getBytesFromFile( Constants.RSA_PRI_FILENAME );

        PrivateKey rsak = Dec.getPrivateKeyFromRawBytes( rsaPriKey );
        byte[] aesKey = Dec.decRsaOAEP( c_rsa_oaepwithsha256andmgf1, rsak );
        SecretKey k = Dec.getAESKey( aesKey );
        byte[] FINALRESULT = Dec.decAESCBC( c_aes_cbc, k, iv );
        String s = new String( FINALRESULT );
        System.out.println( s );
        Constants.writeToFile( s );
    }
}
